package com.qiankan.qmsj.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.qiankan.qmsj.R;
import com.qiankan.qmsj.UserInfo;
import com.qiankan.qmsj.activity.LogInActivity;
import com.qiankan.qmsj.activity.PersionInfoActivity;
import com.qiankan.qmsj.activity.WebActivity;
import com.qiankan.qmsj.db.UserDBHelper;
import com.qiankan.qmsj.entity.Fixproducts;
import com.qiankan.qmsj.utils.AsyncLoadImage;
import com.xingchen.AlertDialog;

import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 */

public class GuessYouLikePagerAdapter extends PagerAdapter {
    private Context context;
    private List<Fixproducts> data;

    public GuessYouLikePagerAdapter(Context context, List<Fixproducts> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView view = new ImageView(context);
        view.setScaleType(ImageView.ScaleType.FIT_CENTER);
        AsyncLoadImage.loadImage(data.get(position).getBanner(), view, R.mipmap.yellow_bg_def);
        container.addView(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserInfo userInfo = UserDBHelper.getInstance(context).getUserInfo();
                if (userInfo == null) {
                    Intent intent = new Intent(context, LogInActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    context.startActivity(intent);
                    return;
                }
                if (TextUtils.isEmpty(userInfo.getName()) || TextUtils.isEmpty(userInfo.getIdcard()) || TextUtils.isEmpty(userInfo.getPhone())) {
                    showDialog();
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("url", data.get(position).getLink());
                //分享相关
                bundle.putString("shareUrl", data.get(position).getLink());
                bundle.putString("title", data.get(position).getName());
                bundle.putString("content", data.get(position).getAdvertising());
                bundle.putString("sharePicUrl", data.get(position).getIcon());
                context.startActivity(WebActivity.newIntent(context, bundle));
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    private void showDialog() {
        new AlertDialog(context).builder().setTitle("提示")
                .setMsg("请完善个人信息 ")
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        context.startActivity(PersionInfoActivity.newIntent(context));
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        }).show();
    }
}
